# Maintainer: Junjie Mao <eternal.n08@gmail.com>

_realname=ocaml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.04.0
pkgrel=1
pkgdesc="An industrial strength programming language supporting functional, imperative and object-oriented styles (mingw-w64)"
arch=('any')
url='https://ocaml.org/'
license=('Q Public Licence 1.0' 'GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-flexdll")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "diffutils")
source=("https://caml.inria.fr/pub/distrib/ocaml-${pkgver%.*}/${_realname}-${pkgver}.tar.gz"
        "0002-Adapt-to-msys2-directory-layout.patch"
        "0003-Install-manuals-in-msys2.patch"
        "0004-camlheader-has-the-extension-.exe-in-msys2.patch"
        "0005-Install-libraries-to-lib-ocaml-instead-of-lib.patch"
        "0006-Configure-the-relative-paths-for-ocaml-libraries-on-.patch"
        "0007-Convert-to-windows-path-before-checking-stats.patch"
        "0008-Use-the-standard-paths-as-default-in-ocamlbuild.patch")
sha256sums=('cb63486f6cb837a5a57ab93e4c429551127561d0da61b7712116769a6a8322de'
            'a229ade97ba801b18bfeba2c549bda8c5e91bacc1e0d00efc0e295f1f046c447'
            '1ee3002e6ae55c09a77d42804cd1b9e30967179d7978d5fcf6120e63bec6aac5'
            '1932626f4bb89233811d2b49ce03f8eecd06a72d6a8e9d3b19afd8193fd59b99'
            '2e1eae8bf4f7564c3e8e634d8f9d541d6878ee29eb7a59b1025a1162d90cbfdc'
            '893363d27ce9e08e891997286ee42303aaa394a0d3a2b62c498eea3d207a6277'
            '117e62270038aac0f7a2737685c04120864ab3a89b816c6e54c5a81cc848293c'
            '5dce92c7b228eee6dda9595288e83b5c47008b530013ca851ebc888700a2ae6a')
install=ocaml-${CARCH}.install

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  mv config/Makefile.mingw{,32}
  cp config/m-nt.h config/m.h
  cp config/s-nt.h config/s.h

  patch -p1 -i ${srcdir}/0002-Adapt-to-msys2-directory-layout.patch
  patch -p1 -i ${srcdir}/0003-Install-manuals-in-msys2.patch
  patch -p1 -i ${srcdir}/0004-camlheader-has-the-extension-.exe-in-msys2.patch
  patch -p1 -i ${srcdir}/0005-Install-libraries-to-lib-ocaml-instead-of-lib.patch
  patch -p1 -i ${srcdir}/0006-Configure-the-relative-paths-for-ocaml-libraries-on-.patch
  patch -p1 -i ${srcdir}/0007-Convert-to-windows-path-before-checking-stats.patch
#  patch -p1 -i ${srcdir}/0008-Use-the-standard-paths-as-default-in-ocamlbuild.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf ${_realname}-${pkgver} build-${MINGW_CHOST}

  unset FLEXDIR
  unset OCAMLDIR
  cd ${srcdir}/build-${MINGW_CHOST}
  cp config/Makefile.${MINGW_PREFIX#/} config/Makefile

  make -f Makefile.nt world TOOLPREF= -j1
  make -f Makefile.nt bootstrap TOOLPREF= -j1
  make -f Makefile.nt opt TOOLPREF= -j1
  make -f Makefile.nt opt.opt TOOLPREF= -j1
}

check() {
  # The testsuite refuses to compile due to lack of headers such as sys/resource.h
  plain "skip"
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make -f Makefile.nt install TOOLPREF= PREFIX="${pkgdir}${MINGW_PREFIX}" -j1
  [ -f LICENSE ] && install -Dm644 LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  mv ${pkgdir}${MINGW_PREFIX}/lib/ocaml/stublibs/*.dll ${pkgdir}${MINGW_PREFIX}/bin
}
